﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;

namespace AlgoLogicMessenger
{
    //Interaction logic for Videos.xaml
    public partial class Videos : Page
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream = default(NetworkStream);

        public Videos()
        {
            InitializeComponent();
            IsPlaying(false);

            clientSocket.Connect(IP.Address, 8888);
            serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("" + "#");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            mediaElement1.Volume = 100;
        }

        private void IsPlaying(bool bValue)
        {
            BtnStop.IsEnabled = bValue;
            BtnPlay.IsEnabled = bValue;
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@back|#");
            if (serverStream != null)
            {
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }

            mediaElement1.Stop();

            VideoFrame.Content = null;
            NavigationPage np = new NavigationPage();
            VideoFrame.Content = np;
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "Video Files (*.avi);(.mp4)|*.avi;*.mp4|PowerPoint(.mp4)|*.mp4";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                mediaTB.Text = ofd.FileName;
                mediaElement1.Source = new Uri(ofd.FileName);

                byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@videoName|" + ofd.SafeFileName + "|#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }

                BtnPlay.IsEnabled = true;
            }
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            IsPlaying(true);
            if (BtnPlay.Content.ToString() == "Play")
            {
                mediaElement1.Play();
                BtnPlay.Content = "Pause";

                byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@videoPlay|Play|#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }
            }
            else
            {
                mediaElement1.Pause();
                BtnPlay.Content = "Play";

                byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@videoPlay|Pause|#");
                if (serverStream != null)
                {
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                }
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Stop();
            BtnPlay.Content = "Play";
            IsPlaying(false);
            BtnPlay.IsEnabled = true;

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@videoPlay|Stop|#");
            if (serverStream != null)
            {
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }
        }

        string text = "";
        private void Page_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F5)
            {
                e.Handled = true;
            }
            else if (e.Key != Key.Back)
            {
                text = e.Key.ToString();
            }
            else if (e.Key == Key.Back)
            {
                if (text != "")
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        private void mediaElement1_MediaEnded(object sender, RoutedEventArgs e)
        {
            BtnPlay.Content = "Play";
            BtnStop.IsEnabled = false;
            mediaElement1.Position = mediaElement1.Position - mediaElement1.Position;
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("@videoPlay|#");
            if (serverStream != null)
            {
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }
            mediaElement1.Stop();
        }

        private void sliderPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            TimeSpan ts = TimeSpan.FromSeconds(e.NewValue);
            tbPosition.Text = String.Format("{0:00}:{1:00}:{2:00}",ts.Hours, ts.Minutes, ts.Seconds);
        }

        private void MediaEL_MediaOpened(object sender, RoutedEventArgs e)
        {
            DispatcherTimer timer;
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(200);
            timer.Tick += new EventHandler(timer_Tick);
            if (mediaElement1.NaturalDuration.HasTimeSpan)
            {
                TimeSpan ts = mediaElement1.NaturalDuration.TimeSpan;
                seekBar.Maximum = ts.TotalSeconds;
                seekBar.SmallChange = 1;
                seekBar.LargeChange = Math.Min(10, ts.Seconds / 10);

                tbPosition.Text = String.Format("00:00:00");
                tbDuration.Text = String.Format("{0:00}:{1:00}:{2:00}",
                ts.Hours, ts.Minutes, ts.Seconds);
            }
            timer.Start();
        }

        bool isDragging = false;

        void timer_Tick(object sender, EventArgs e)
        {
            if (!isDragging)
            {
                seekBar.Value = mediaElement1.Position.TotalSeconds;
            }
        }

        private void seekBar_DragStarted(object sender, DragStartedEventArgs e)
        {
            isDragging = true;
        }

        private void seekBar_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            isDragging = false;
            mediaElement1.Position = TimeSpan.FromSeconds(seekBar.Value);
        }

        private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            mediaElement1.Volume = (double)volumeSlider.Value;
        }

        void InitializePropertyValues()
        {
            mediaElement1.Volume = (double)volumeSlider.Value;
        }
    }
}